// pages/htclockin/htclockin.js
// 获取应用实例
var util = require('../../utils/util.js')
var myDate = new Date()
var htList = []

Page({
  data: {
    htInfo: [],
    currentDate: "",
    ishealthy: '',
    allowEdit: true,
    showhealthy: '',
    isfever: '',
    showfever: '',
    isrisk: '',
    showrisk: '',
    healthycode: '',
    showhealthycode: '',
    temperature: '',
    temperatureshow: false,
    columns1: ['是', '否'],
    columns2: ['否', '是'],
    isriskshow: false,
    codeclumns: ['绿', '黄', '红'],
    healthycodeshow: false,
    Nowdate: '',
    showCalendar: false,
    maxDate:null,
    minDate: null,
  },
  ishealthyPop() {
    this.setData({
      temperatureshow: true
    });
  },
  isriskPop() {
    this.setData({
      isriskshow: true
    });
  },
  healthyCodePop() {
    this.setData({
      healthycodeshow: true
    })
  },

  CloseIshealthy() {
    this.setData({
      temperatureshow: false
    });
  },
  Closehealthycode() {
    this.setData({
      healthycodeshow: false
    });
  },

  onConfirmIshealthy(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
    this.setData({
      showhealthy: value,
      ishealthy: index,
      temperatureshow: false
    })
  },
  onConfirmrisk(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
    this.setData({
      showrisk: value,
      isrisk: index,
      isriskshow: false
    })
  },
  onConfirmcode(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
    this.setData({
      showhealthycode: value,
      healthycode: index,
      healthycodeshow: false,
    })
  },
  ontemperature(event) {
    if (event.detail.value > 30 && event.detail.value < 41) {
      this.setData({
        temperature: event.detail.value
      })
      if (event.detail.value < 37.3) {
        this.setData({
          isfever: 0
        })
      } else {
        (
          this.setData({
            isfever: 1
          })
        )
      }
    } else {
      wx.showToast({
        title: '请重新输入体温,体温范围30-41',
        icon: 'error',
        duration: 2000
      })
    }
  },

  onCancelIshealthy() {
    this.setData({
      temperatureshow: false
    })
  },
  onCancelrisk() {
    this.setData({
      isriskshow: false
    })
  },
  onCancelcode() {
    this.setData({
      healthycodeshow: false
    })
  },

  onLoad: function () {
    console.log("加载函数")
    var that = this
    var date = myDate.toLocaleDateString();
    date = util.formatTime(new Date(), "Y-M-D")
    console.log(date)
    this.setData({
      currentDate: date
    });
    console.log(this.data.currentDate)
    //设置查询日期限制
    let dateTime = new Date();
    dateTime = dateTime.setDate(dateTime.getDate() - 30);
    dateTime = new Date(dateTime);
    console.log(dateTime)
    this.setData({
      date: date,
      minDate: dateTime.getTime(),
    });
    wx.request({
      url: 'http://localhost/Wx/NowhtHty',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        openid: wx.getStorageSync('openid'),
        NowDate: date
      },
      success(res) {
        console.log(res.data)
        that.setData({
          Nowdate: date,
          htInfo: res.data,
          code: res.data.code
        })

      },
      fail(res) {
        console.log(res.Date)
      }
    })
  },

  getHistroyHty(event) {
    console.log("日历显示函数")
    console.log("calendar")
    this.setData({
      maxDate:new Date().getTime(),
      showCalendar: true
    })
  },
  onCalendarConfirm(event) {
    console.log("日历确认函数")
    var queryDate = event.detail
    queryDate = util.formatTime(queryDate, "Y-M-D")
    console.log(queryDate)
    this.setData({
      date: queryDate
    });
    let _that = this
    wx.request({
      url: 'http://localhost/Wx/NowhtHty',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        openid: wx.getStorageSync('openid'),
        NowDate: queryDate
      },
      success(res) {
        console.log(res.data)
        _that.setData({
          Nowdate: queryDate,
          htInfo: res.data,
          code: res.data.code
        })

      },
      fail(res) {
        console.log(res.Date)
      }
    })
    _that.setData({
      showCalendar: false,
      allowEdit: false
    })
    console.log(_that.data.currentDate)
    console.log(_that.data.date)
    if (_that.data.currentDate == _that.data.date) {
      console.log("为本日")
      _that.setData({
        allowEdit: true
      })
    }

  },
  onCalendarClose() {
    console.log("日历关闭函数")
    this.setData({
      showCalendar: false
    })
  },
  onClickhtdaka() {
    console.log("打卡提交函数")
    if (this.ishealthy != '' && this.isrisk != '' && this.temperature != '' && this.healthycode != '' && this.htInfo == null) {
      console.log(this.data.ishealthy)
      console.log(this.data.isrisk)
      console.log(this.data.temperature)
      console.log(this.data.healthycode)
      console.log(this.data.Nowdate)
      // if(this.temperature>=30&&this.temperature<=41){
      wx.request({

        url: 'http://localhost/Wx/htSubmit',
        method: 'post',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: {
          openid: wx.getStorageSync('openid'),
          Nowdate: this.data.Nowdate,
          temperature: this.data.temperature,
          ishealthy: this.data.ishealthy,
          isrisk: this.data.isrisk,
          healthycode: this.data.healthycode,
          isfever: this.data.isfever
        },
        success(res) {
          if (res.data.code == 200) {
            wx.showToast({
                title: '提交成功',
                icon: 'succes',
                duration: 1000,
                mask: true
              }),
              wx.navigateTo({
                url: './htclockin',
              })
          } else {
            wx.showToast({
              title: '请求失败,请检查数据',
              icon: 'fail',
              duration: 1000,
              mask: true
            })
          }
        },
      })
    } else {
      console.log("当前提交数据有误")
    }
    // }else{
    //   console.log("提交数据为空")
    // }
  }
})